export interface ChatItem {
  id: string
  content: string
  isAnswer: boolean
  message_files?: MessageFile[]
  agent_thoughts?: any
  feedback?: Feedbacktype
  annotation?: Annotation
  workflow_run_id?: string
  workflowProcess?: {
    status: WorkflowRunningStatus
    tracing: any[]
  }
  streaming?: boolean
  lastUpdated?: number
}

export interface MessageFile {
  id: string
  type: string
  url: string
  belongs_to: 'user' | 'assistant'
}

export interface ConversationItem {
  id: string
  name: string
  created_at?: string
  updated_at?: string
}

export interface VisionFile {
  upload_file_id?: string
  type: string
  url: string
  transfer_method?: TransferMethod
}

export interface VisionSettings {
  enabled: boolean
  number_limits: number
  detail: Resolution
  transfer_methods: TransferMethod[]
}

export interface PromptConfig {
  prompt_template: string
  prompt_variables: PromptVariable[]
  opening_statement?: string
  suggested_questions?: string[]
  suggested_questions_after_answer?: SuggestedQuestionsAfterAnswerConfig
  speech_to_text?: SpeechToTextConfig
  text_to_speech?: TextToSpeechConfig
  retriever_resource?: RetrieverResourceConfig
  annotation_reply?: AnnotationReplyConfig
  more_like_this?: MoreLikeThisConfig
  user_input_form?: UserInputFormConfig[]
  dataset_query_variable?: string
  pre_prompt?: string
  agent_mode?: AgentConfig
  chat_prompt_config?: any
  completion_prompt_config?: any
  dataset_configs?: DatasetConfigs
  external_data_tools?: ExternalDataTool[]
  file_upload?: FileUploadConfig
}

export interface PromptVariable {
  key: string
  name: string
  type: string
  required: boolean
  options?: string[]
}

export interface UserInputFormConfig {
  text_input?: {
    label: string
    variable: string
    required: boolean
    default?: string
  }
  select?: {
    label: string
    variable: string
    required: boolean
    default?: string
    options: string[]
  }
  paragraph?: {
    label: string
    variable: string
    required: boolean
    default?: string
  }
}

export enum Resolution {
  low = 'low',
  high = 'high'
}

export enum TransferMethod {
  remote_url = 'remote_url',
  local_file = 'local_file'
}

export enum WorkflowRunningStatus {
  Running = 'running',
  Succeeded = 'succeeded',
  Failed = 'failed',
  Stopped = 'stopped'
}

export interface SuggestedQuestionsAfterAnswerConfig {
  enabled: boolean
}

export interface SpeechToTextConfig {
  enabled: boolean
}

export interface TextToSpeechConfig {
  enabled: boolean
  voice?: string
  language?: string
}

export interface RetrieverResourceConfig {
  enabled: boolean
}

export interface AnnotationReplyConfig {
  enabled: boolean
}

export interface MoreLikeThisConfig {
  enabled: boolean
}

export interface AgentConfig {
  enabled: boolean
  strategy?: string
  tools?: any[]
}

export interface DatasetConfigs {
  retrieval_model: string
  datasets: {
    dataset: {
      id: string
    }
  }[]
}

export interface ExternalDataTool {
  enabled: boolean
  type: string
  config: any
}

export interface FileUploadConfig {
  image: {
    enabled: boolean
    number_limits: number
    detail: Resolution
    transfer_methods: TransferMethod[]
  }
}

export type LogAnnotation = {
  content: string
  account: {
    id: string
    name: string
    email: string
  }
  created_at: number
}

export type Annotation = {
  id: string
  authorName: string
  logAnnotation?: LogAnnotation
  created_at?: number
}

export const MessageRatings = ['like', 'dislike', null] as const
export type MessageRating = typeof MessageRatings[number]

export type MessageMore = {
  time: string
  tokens: number
  latency: number | string
}

export type Feedbacktype = {
  rating: MessageRating
  content?: string | null
}

export type FeedbackFunc = (messageId: string, feedback: Feedbacktype) => Promise<any>
export type SubmitAnnotationFunc = (messageId: string, content: string) => Promise<any>

export type DisplayScene = 'web' | 'console'

export type ToolInfoInThought = {
  name: string
  input: string
  output: string
  isFinished: boolean
}

export type ThoughtItem = {
  id: string
  tool: string // plugin or dataset. May has multi.
  thought: string
  tool_input: string
  message_id: string
  observation: string
  position: number
  files?: string[]
  message_files?: VisionFile[]
}

export type CitationItem = {
  content: string
  data_source_type: string
  dataset_name: string
  dataset_id: string
  document_id: string
  document_name: string
  hit_count: number
  index_node_hash: string
  segment_id: string
  segment_position: number
  score: number
  word_count: number
}

export type IChatItem = {
  id: string
  content: string
  citation?: CitationItem[]
  /**
   * Specific message type
   */
  isAnswer: boolean
  /**
   * The user feedback result of this message
   */
  feedback?: Feedbacktype
  /**
   * The admin feedback result of this message
   */
  adminFeedback?: Feedbacktype
  /**
   * Whether to hide the feedback area
   */
  feedbackDisabled?: boolean
  /**
   * More information about this message
   */
  more?: MessageMore
  annotation?: Annotation
  useCurrentUserAvatar?: boolean
  isOpeningStatement?: boolean
  suggestedQuestions?: string[]
  log?: { role: string; text: string }[]
  agent_thoughts?: ThoughtItem[]
  message_files?: VisionFile[]
}

export type MessageEnd = {
  id: string
  metadata: {
    retriever_resources?: CitationItem[]
    annotation_reply: {
      id: string
      account: {
        id: string
        name: string
      }
    }
    files?: any[]
  }
  files?: any[]
  message_files?: VisionFile[]
  data?: {
    files?: any[]
    [key: string]: any
  }
  [key: string]: any
}

export type MessageReplace = {
  id: string
  task_id: string
  answer: string
  conversation_id: string
}

export type AnnotationReply = {
  id: string
  task_id: string
  answer: string
  conversation_id: string
  annotation_id: string
  annotation_author_name: string
}
